<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    使用javascript编写函数，能够对传入的常见对象进行clone(深拷贝)

    1、通过JSON的stringify与parse方法实现

    2、通过递归的方法实现
</body>
<script>
    // 方法一
    let obj = { a: 1, b: [{ c: 2, d: 3 }, { e: 4, f: 5 }], g: 6 }
    let result = JSON.parse(JSON.stringify(obj));

    console.log(result, obj, result === obj);
    console.log(result.b, obj.b, result.b === obj.b);
    console.log(result.b[0], obj.b[0], result.b[0] === obj.b[0]);


    // 方法二
    const deepCopy = (obj) => {
    let target  = null
    if(typeof obj === 'object'){
        if(Array.isArray(obj)){ //数组
            target = [];
            obj.forEach(item => {
                target.push(deepCopy(item));
            })
        }else if(obj){
            target = {}
            let objKeys = Object.keys(obj);
            objKeys.forEach(key => {
                target[key] = deepCopy(obj[key]);
            })
        }else{
            target = obj
        }
    }else{
        target = obj;
    }
    return target
}
 
obj = {a: 1, b: [{c: 2, d: 3}, {e: 4, f: 5}], g: 6}
result = deepCopy(obj);
console.log(result, obj, result === obj)             
console.log(result.b, obj.b, result.b === obj.b)     
console.log(result.b[0], obj.b[0], result.b[0] === obj.b[0])



    function deepClone(source) {
        if (!source) return
        let target;
        if (typeof source === 'object') {
            // 根据source类型判断是新建一个数组还是对象
            target = Array.isArray(source) ? [] : {}
            // 遍历source，并且判断是source的属性才拷贝
            for (let key in source) {
                if (source.hasOwnProperty(key)) {
                    if (typeof source[key] !== 'object') {
                        target[key] = source[key]
                    } else {
                        // 如果内部属性存在复杂数据类型，使用递归实现深拷贝
                        target[key] = deepClone(source[key])
                    }
                }
            }
        } else {
            target = source
        }
        return target
    }



    //方法三
    const deepCopy1 = (obj) => {
    //浅拷贝子节点
    let handleChild = (child) => {
        if(typeof child === 'object'){
            if(Array.isArray(child)){ // 数组
            return [...child]
            }else if(child){ // 对象
            return {...child}
            }else{ // null
            return child
            }
        }else{ // 值类型
            return child
        }
    }
    let arr = [];
    let target = {result: obj};
    let current = target; 
    while(current){
    if(typeof current === 'object'){
        if(Array.isArray(current)){ //数组
        current.forEach((item, index) => {
            let child = handleChild(item)
            current[index] = child;
            arr.push(child);
        })
        }else if(current){ //对象
        let objKeys = Object.keys(current);
        objKeys.forEach(key => {
            let child = handleChild(current[key]);
            current[key] = child;
            arr.push(child);
        })
        }else{ //null
        temp = current;
        }
    }
    current = arr.shift()
    }
    return target.result
}
 
obj = {a: 1, b: [{c: 2, d: 3}, {e: 4, f: 5}], g: 6}
result = deepCopy1(obj);
console.log(result, obj, result === obj)             
console.log(result.b, obj.b, result.b === obj.b)     
console.log(result.b[0], obj.b[0], result.b[0] === obj.b[0])
</script>

</html>